home *** CD-ROM | disk | FTP | other *** search
- #include "patches.h"
- #include <traps.h>
- #include <lowmem.h>
- #include "MacsBugSerialStuff.h"
- #include "MBPackets.h"
- #include <iostream>
-
- static MBPacket gHeader;
- static EventRecord gEvent;
- static long gPos = 0;
-
- enum{
- modeHeader = 0,
- modeEvent = 1
- };
-
- static long gMode = modeHeader;
-
-
- static asm long* GetOldDebugUtilStorage()
- {
- lea _oldDebugUtilStorage,a0
- rts
- _oldDebugUtilStorage:
- dc.l 0x00000000
- }
-
- static asm long* GetCountStorage()
- {
- lea _countStorage,a0
- rts
- _countStorage:
- dc.l 0x00000000
- }
-
-
- static asm long* GetValueStorage()
- {
- lea _valueStorage,a0
- rts
- _valueStorage:
- dc.l 0x00000000
- }
-
-
- static asm long* GetA5Storage()
- {
- lea _a5Store,a0
- rts
- _a5Store:
- dc.l 0x00000000
- }
-
-
- static Boolean KeyIsDown(
- short theKeyCode)
- {
- KeyMap theKeys;
-
- GetKeys( theKeys); /* Get state of each key */
-
- /* Ordering of bits in a KeyMap is truly bizarre. A KeyMap is a */
- /* 16-byte (128 bits) array where each bit specifies the start */
- /* of a key (0 = up, 1 = down). We isolate the bit for the */
- /* specified key code by first determining the byte position in */
- /* the KeyMap and then the bit position within that byte. */
- /* Key codes 0-7 are in the first byte (offset 0 from the */
- /* start), codes 8-15 are in the second, etc. The BitTst() trap */
- /* counts bits starting from the high-order bit of the byte. */
- /* For example, for key code 58 (the option key), we look at */
- /* the 8th byte (7 offset from the first byte) and the 5th bit */
- /* within that byte. */
-
- return( BitTst( ((char*) &theKeys) + theKeyCode / 8,
- (long) 7 - (theKeyCode % 8) ) );
- }
-
- // 16777216
-
- static void memset(Ptr p,char value,long length)
- {
- long z = 0;
- for(z = 0;z<length;z++){
- p[z] = value;
- }
- }
-
- static Ptr GetScreenBase()
- {
- GDHandle h = LMGetMainDevice();
-
- return h[0]->gdPMap[0]->baseAddr;
- }
-
- static OSErr gLastError = 0;
-
- static void ReceiveData(long inMBA5)
- {
- if(SerialDriver_BytePresent()){
- UInt8 theByte;
-
- if(SerialDriver_ReceiveByte(&theByte,10) == noErr){
-
-
- switch(gMode){
- case modeHeader:
- Ptr p = (Ptr)&gHeader;
- p[gPos++] = theByte;
- if(gPos == sizeof(gHeader)){
- if(gHeader.magic == kMBMagic){
- gMode = modeEvent;
- }
-
- gPos = 0;
- }
-
- break;
-
- case modeEvent:
- // take modifiers you want ROR.W stuff into KeyMap+6
-
- Ptr p1 = (Ptr)&gEvent;
- p1[gPos++] = theByte;
- if(gPos == sizeof(gEvent)){
-
- short what = gEvent.what;
- long message = gEvent.message;
-
- if(what == autoKey){
- what = keyDown;
- }
-
- Ptr p = GetScreenBase();
- *p = (*p)?0:0xFF;
- long myA5 = SetA5(inMBA5);
- short modifiers = gEvent.modifiers;
- modifiers = modifiers >> 1;
- short* keyMap = (short*)(0x174);
- keyMap[4] = modifiers;
-
- OSErr err = PostEvent(what,message);
- PostEvent(keyUp,0);
- SetA5(myA5);
- if(err != noErr){
- gLastError = err;
- }
- // if(gEvent.what == keyDown){
- // }
- // }
- gPos = 0;
- gMode = modeHeader;
- }
- break;
- }
-
- }
- }
- }
-
-
- static long Poll(short selector)
- {
- long continueQ = true;
- long* theA5 = GetA5Storage();
-
- long mbA5 = SetA5(*theA5);
-
- if(selector == 3){
- ReceiveData(mbA5);
- }
-
- return continueQ;
- }
-
-
- static asm void newDebugUtil()
- {
- subq.l #4,a7 // reserve space for old trap address
- move.l a0,-(a7) // save a0
- movem.l a1-a5/d0-d7,-(a7) // save everything else
-
- move.w d0,-(a7)
- jsr Poll
- addq.l #2,a7
- tst.w d0
- bne.s _Continue
-
- movem.l (a7)+,a1-a5/d0-d7
- move.l (a7)+,a0
- addq.l #4,a7
- rts
-
- _Continue:
-
-
- movem.l (a7)+,a1-a5/d0-d7 // restore registers
-
- jsr GetOldDebugUtilStorage // get original trap address storage in a0
- move.l (a0),a0 // get original trap address value into a0
- move.l a0,4(a7) // stuff old trap onto stack
- move.l (a7)+,a0 // restore original a0
- rts // rts to jump to old trap.
- }
-
- void main()
- {
-
- long* theA5 = GetA5Storage();
- *theA5 = SetCurrentA5();
-
- long* debugUtilStorage = GetOldDebugUtilStorage();
- *debugUtilStorage = PatchTrap(_DebugUtil,(long)newDebugUtil);
-
-
- SerialDriver_Open();
-
- Debugger();
-
- cout << "Error: " << gLastError << endl;
-
-
- }